package com.rivulet.dawn.service.service.impl;

import com.rivulet.dawn.api.model.BraveModel;
import com.rivulet.dawn.api.request.AllDeleteRequest;
import com.rivulet.dawn.api.request.BraveQueryRequest;
import com.rivulet.dawn.api.response.BraveDetailResponse;
import com.rivulet.dawn.api.response.BraveSearchResponse;
import com.rivulet.dawn.api.tools.Page;
import com.rivulet.dawn.service.mapper.BraveMapper;
import com.rivulet.dawn.service.service.BraveService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * @author: xjc
 * @date: 2022/8/12 17:44
 **/
@Service
public class BraveServiceImpl implements BraveService {
    @Resource
    private BraveMapper braveMapper;

    @Override
    public Integer create(BraveModel braveModel) {
        return braveMapper.insert(braveModel);
    }

    @Override
    public void delete(AllDeleteRequest request) {

    }

    @Override
    public Integer update(BraveModel braveModel) {
        return null;
    }

    @Override
    public Page<BraveSearchResponse> search(BraveQueryRequest request) {
        List<BraveModel> modelList = braveMapper.search(request);
        List<BraveSearchResponse> respList = new ArrayList<>();
        for (BraveModel model : modelList) {
            BraveSearchResponse resp = new BraveSearchResponse();
            BeanUtils.copyProperties(model, resp);
            respList.add(resp);
        }
        return new Page<>(Long.valueOf(respList.size()), respList);
    }

    @Override
    public BraveDetailResponse detail(Long id) {
        /*通过id查询勇者对象，再根据勇者对象将对应数据复制到response中*/
        BraveModel model = braveMapper.selectById(id);
        BraveDetailResponse response = new BraveDetailResponse();
        if (Objects.nonNull(model)) {
            BeanUtils.copyProperties(model, response);
        }
        return response;
    }

}
